<?php namespace App\Core\Repository\Criterias\Filters;

use App\Core\Repository\Criteria;
use App\Core\Repository\Repository;
use Input;

class InputTimeSinceCriteria extends Criteria{
    
    protected $field;
    protected $input;

    public function __construct($field, $input = '', $default = '') {
        $this->field = $field;
        $this->input = Input::get($input ? $input : $field, $default);
        $this->setName('filter_time_since_'.$this->field);
    }

    public function apply(Repository $repository){
        parent::apply($repository);
        $time = $this->getTimeFromInput();
        if($time){
            $field = $this->formatField($this->model, $this->field);
            $this->query->where($this->field, '>', strtotime(date('Y-m-d', time() - $time)));
        }
    }
    
    protected function getTimeFromInput(){
        $time = 0;
        switch ($this->input) {
            case 'week' : 
                $time = 3600*24*7;
                break;
            case 'month' :
                $time = 3600*24*30;
                break;
            case '3month' : 
                $time = 3600*24*90;
                break;
            case 'year' :
                $time = 3600*24*365;
                break;
            case 'all' : 
        }
        return $time;
    }


    protected function validate(){
        if($this->input){
            return in_array($this->input, ['week', 'month', '3month', 'year', 'all']);
        }
        return parent::validate();
    }
}